<!--
 * @Descripttion: 
 * @version: 
 * @Author: cyj
 * @Date: 2022-12-05 15:10:31
-->
<template>
    <div class="searchs">
        <!-- 输入框 -->
        <el-input v-model.trim="searchWord" placeholder="请输入内容" prefix-icon="Search" class="inputs" ref="buttonRef"
            @keyup.enter="search" />
        <!-- 弹出框 -->
        <!-- 先用点击凑合一下，focus不行，不懂 -->
        <el-popover width="350" placement="bottom" trigger='click' ref="popoverRef" :virtual-ref="buttonRef"
            virtual-triggering>
            <div class="hotlist">
                <!-- 搜索历史 -->
                <div class="history" v-if="searchList.length">
                    <div class="history-title">
                        <span>搜索历史</span>
                        <el-icon class="delete-icon" size="17" @click="clearAll">
                            <Delete />
                        </el-icon>
                    </div>
                    <div class="history-list">
                        <el-tag class="tag" v-for="(item, index) in searchList" type='info' :key="index" effect="plain"
                            round closable @close="clearTag(item)" @click="gotoSearch(item)">
                            {{ item }}
                        </el-tag>
                    </div>
                </div>

                <!-- 热搜榜 -->
                <div class="hot">
                    <div class="hot-title">热搜榜</div>

                    <div class="hot-list" v-for="(item, index) in HotList.HotList" :key="index">
                        <div class="list-left" :class="(index < 3) ? 'hotword-num' : ''"><span>{{ (index + 1) }}</span>
                        </div>
                        <div class="list-right">
                            <div class="searchName">
                                <span class="word" :class="(index < 3) ? 'hotword' : ''">{{ item.searchWord }}</span>
                                <span class="icon" v-if="item.iconUrl">
                                    <img :src="item.iconUrl" alt="">
                                </span>
                                <span class="num">{{ item.score }}</span>
                            </div>
                            <div class="info" v-if="item.content">{{ item.content }}</div>
                        </div>
                    </div>
                </div>
            </div>
        </el-popover>

    </div>
</template>

<script setup>
import { ref, onMounted, reactive, getCurrentInstance } from 'vue'
import { useRouter } from 'vue-router'
import { storeToRefs } from 'pinia'
import { useHotListStore } from '../store/hotList'
import { getHotList } from '../api/searchdetail/searchdetail'
import { ElMessage } from 'element-plus'
//数据区
const buttonRef = ref()
const popoverRef = ref()
const router = useRouter()
const instance = getCurrentInstance()
let searchWord = ref('')
//创建仓库的实例，可以获取到该仓库的数据
const HotList = useHotListStore()

//解构出搜索历史数组
let { searchList } = storeToRefs(HotList)
//数据区


//方法区

function gotoSearch(keyword) {
    searchWord.value = keyword
    search()  //调用search搜索
    instance.refs.popoverRef.hide()  //隐藏弹框
}

//获取热搜数据
async function getHotLists() {
    let res = await getHotList()
    if (res.status === 200) {
        //存到pinia仓库中
        HotList.HotList = res.data.data
    }
}
onMounted(() => {
    getHotLists()
})

//搜索关键词
function search() {
    //如果用户没有输入
    if (searchWord.value.trim() == '') {
        ElMessage({
            message: '输入内容不能为空',
            showClose: true,
            type: 'warning',
            center: true
        })
    } else {
        //跳转到搜索详情页面，重定向为歌曲搜索结果
        router.push('/searchdetail/' + searchWord.value)
        //添加进搜索历史数组
        addHistory(searchWord.value)
    }
}

//添加进搜索历史数组 同时保存在仓库中
function addHistory(keyword) {
    //这里只写searchList不行，因为解构出来的是ref对象!!!
    if (searchList.value.indexOf(keyword) == -1) {
        //如果没有重复就添加进去
        searchList.value.unshift(keyword)
        // //追加的方式添加
        // HotList.searchList = searchList
    }
}


// 清空搜索历史
function clearAll() {
    searchList.value = []
}

//删除某个标签 直接作用于仓库 
function clearTag(ele) {
    searchList.value = searchList.value.filter((item) => {
        return item != ele
    })
}
//方法区

</script>

<style lang="scss" scoped>
.searchs {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
}

.hotlist {
    width: 337px;
    height: 450px;
    //超过的部分，使用滑动条
    overflow-y: scroll;

    .history {
        margin-bottom: 20px;

        .history-title {
            font-size: 15px;
            display: flex;
            align-items: center;

            .delete-icon {
                cursor: pointer;
                margin-left: 5px;
                margin-right: 5px;
            }
        }

        &-list {
            .tag {
                cursor: pointer;
                margin-top: 10px;
                color: black;
                font-size: 12px;
                margin-right: 10px;
            }

            .tag:hover {
                background-color: #ececec;
            }
        }
    }

    .hot {
        &-title {
            font-size: 15px;
            margin-bottom: 10px;
        }

        &-list {
            cursor: pointer;
            display: flex;
            margin-bottom: 10px;

            .hotword-num {
                color: red;
                font-weight: 700;
            }

            .list-left {
                // height: 50px;
                // line-height: 40px;
                width: 30px;
                margin-right: 15px;
                display: flex;
                align-items: center;
                justify-content: center;

                span {
                    font-size: 18px;
                }
            }

            .list-right {
                .searchName {
                    margin-bottom: 8px;

                    span {
                        margin-right: 10px;
                    }

                    .hotword {
                        font-weight: 700;
                    }

                    .word {
                        font-size: 16px;
                    }

                    .icon {
                        display: inline-block;
                        width: 20px;
                        height: 15px;
                        margin-right: 10px;

                        img {
                            height: 100%;
                            width: 100%;
                        }
                    }


                    .num {
                        color: #ccc;
                    }
                }

                .info {
                    padding-top: 3px;
                    font-size: 11px;
                    color: gray;
                    padding-bottom: 5px;
                }
            }
        }

        &-list:hover {
            background: rgba(220, 220, 220, 0.4);
        }
    }

}


//调整输入框输入时的显示效果
</style>